﻿using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using Clipping.Infrastructure.Interfaces;
using Clipping.Infrastructure.Models;

namespace Clipping.SearchEnginePageParsers
{



    public class YahooNewsPageParser : ISearchEnginePageParse
    {
        public void Parse(string html, out List<Infrastructure.Models.News> news, out string nextPageUrl)
        {
            news = new List<News>();
            nextPageUrl = null;
            HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
            doc.LoadHtml(html);
            var liNav = doc.CreateNavigator();

            var divSearchResult = liNav.Select("//li[@class='record']");
            while (divSearchResult.MoveNext())
            {
                News n = new News();

                var li = divSearchResult.Current;
                //取标题和链接
                var linkNode = li.SelectSingleNode("descendant::h3/a");
                if (linkNode != null)
                {
					n.Title = n.Title = n.Title =System.Web.HttpUtility.HtmlDecode(linkNode.Value);

                    n.Url =System.Web.HttpUtility.UrlDecode( linkNode.GetAttribute("href", ""));
                }

                //取媒体
                var mediaNode = li.SelectSingleNode("descendant::span[@class='url']");
                if (mediaNode != null)
                    n.Media = mediaNode.Value;


                var dateNode = li.SelectSingleNode("descendant::span[@class='date']");
                if (dateNode != null)
                {
                    string dateStr = dateNode.Value;
                    DateTime dt;
                    if (DateTime.TryParse(dateStr, out dt))
                    {
                        n.PubDate = dateStr;
                    }
                    else
                    {
                        var hourMatch = Regex.Match(dateStr, @"(\d+)\s*小时前", RegexOptions.None);
                        if (hourMatch.Success)
                        {
                            string hoursStr = hourMatch.Groups[1].Value;
                            int hour = int.Parse(hoursStr);
                            n.PubDate = DateTime.Now.AddHours(0 - hour).ToShortDateString();
                        }
                        else
                        {
                            var minuteMatch = Regex.Match(dateStr, @"(\d+)\s*分钟前", RegexOptions.None);
                            if (minuteMatch.Success)
                            {
								string minuteStr = minuteMatch.Groups[1].Value;
                                int minute = int.Parse(minuteStr);
                                n.PubDate = DateTime.Now.AddMinutes(0 - minute).ToShortDateString();
                            }
                        }
                    }
                }



                //取摘要和相同新闻链接
                var summaryAndSameNewsNode = li.SelectSingleNode("descendant::div[@class='desc']");
                if (summaryAndSameNewsNode != null)
                {
                    string summary = summaryAndSameNewsNode.Value;
                    var sameNewsNodes = summaryAndSameNewsNode.Select("descendant::a");
                    while (sameNewsNodes.MoveNext())
                    {
                        var sameNewsNode = sameNewsNodes.Current;
                        if (sameNewsNode.Value.Contains("相同新闻"))
                        {
                            string sameNewsUrl =System.Web.HttpUtility.HtmlDecode( sameNewsNode.GetAttribute("href", ""));
                            string sameNewsTitle = sameNewsNode.Value;
                            summary = summary.Replace(sameNewsTitle, "");
                            n.SameNewsUrl = sameNewsUrl;
                            break;
                        }
                    }
                    summary = System.Web.HttpUtility.HtmlDecode(summary).Trim();
                    n.Summary = summary;
                }

                news.Add(n);

            }

            //取下一页地址
            var nextPageNode = liNav.SelectSingleNode("//a[@class='btn btn_next']");
            if (nextPageNode != null && nextPageNode.Value != null && nextPageNode.Value.Contains("下一页"))
            {
                nextPageUrl = nextPageNode.GetAttribute("href", "");
                if (!string.IsNullOrEmpty(nextPageUrl))
                    nextPageUrl = System.Web.HttpUtility.HtmlDecode(nextPageUrl);
            }
        }
    }
}
